﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using Tools;
using Wp.DAL;

namespace Wp.BLL
{
    /// <summary>
    /// Summary description for TblTermRelationships
    /// </summary>
    public class TblTermRelationshipsManagerBase
    {
        public TblTermRelationshipsManagerBase()
        {
            //
            // TODO: Add constructor logic here
            //
        }
        public void InsertUpdate(TblTermRelationshipsEntity item)
        {
            List<SqlParameter> ls = new List<SqlParameter>();
            ls.Add(new SqlParameter("@ObjectId", item.ObjectId));
            ls.Add(new SqlParameter("@TermTaxonomyId", item.TermTaxonomyId));
            SqlParameter[] sqlparam = ls.ToArray();
            SqlHelper.ExecuteNonQuery(SqlHelper.WpConnectString, CommandType.StoredProcedure, "wp_InsertUpdateTblTermRelationshipSingle", sqlparam);
        }
        public void InsertUpdate(int ObjectId, int TermTaxonomyId)
        {
            List<SqlParameter> ls = new List<SqlParameter>();
            ls.Add(new SqlParameter("@ObjectId", ObjectId));
            ls.Add(new SqlParameter("@TermTaxonomyId", TermTaxonomyId));
            SqlParameter[] sqlparam = ls.ToArray();
            SqlHelper.ExecuteNonQuery(SqlHelper.WpConnectString, CommandType.StoredProcedure, "wp_InsertUpdateTblTermRelationshipSingle", sqlparam);
        }
        public void Delete(int ObjectId, int TermTaxonomyId)
        {
            List<SqlParameter> ls = new List<SqlParameter>();
            ls.Add(new SqlParameter("@ObjectId", ObjectId));
            ls.Add(new SqlParameter("@TermTaxonomyId", TermTaxonomyId));
            SqlParameter[] sqlparam = ls.ToArray();
            SqlHelper.ExecuteNonQuery(SqlHelper.WpConnectString, CommandType.StoredProcedure, "wp_DeleteTblTermRelationship", sqlparam);
        }


        public TblTermRelationshipsEntity GetTblTermRelationshipsEntity(DataRow dr)
        {
            TblTermRelationshipsEntity item = new TblTermRelationshipsEntity();
            if (dr.Table.Columns.Contains("ObjectId"))
                item.ObjectId = dr["ObjectId"].ToInt();
            if (dr.Table.Columns.Contains("TermTaxonomyId"))
                item.TermTaxonomyId = dr["TermTaxonomyId"].ToInt();
            if (dr.Table.Columns.Contains("Name"))
                item.Name = dr["Name"].ToString();
            if (dr.Table.Columns.Contains("Slug"))
                item.Slug = dr["Slug"].ToString();
            return item;
        }
        public DataTable SelectOneRDT(int ObjectId, int TermTaxonomyId)
        {
            List<SqlParameter> ls = new List<SqlParameter>();
            ls.Add(new SqlParameter("@ObjectId", ObjectId));
            ls.Add(new SqlParameter("@TermTaxonomyId", TermTaxonomyId));
            SqlParameter[] sqlparam = ls.ToArray();
            return SqlHelper.ExecuteDataTable(SqlHelper.WpConnectString, CommandType.StoredProcedure, "wp_SelectTblTermRelationship", sqlparam);
        }
        public TblTermRelationshipsEntity SelectOne(int ObjectId, int TermTaxonomyId)
        {
            List<SqlParameter> ls = new List<SqlParameter>();
            ls.Add(new SqlParameter("@ObjectId", ObjectId));
            ls.Add(new SqlParameter("@TermTaxonomyId", TermTaxonomyId));
            SqlParameter[] sqlparam = ls.ToArray();
            DataTable dt = SqlHelper.ExecuteDataTable(SqlHelper.WpConnectString, CommandType.StoredProcedure, "wp_SelectTblTermRelationship", sqlparam);
            if (dt != null && dt.Rows.Count > 0)
                return GetTblTermRelationshipsEntity(dt.Rows[0]);
            return null;
        }
        public List<TblTermRelationshipsEntity> SelectByObjectId(int ObjectId)
        {
            List<TblTermRelationshipsEntity> lsresult = new List<TblTermRelationshipsEntity>();
            List<SqlParameter> ls = new List<SqlParameter>();
            ls.Add(new SqlParameter("@ObjectId", ObjectId));
            //ls.Add(new SqlParameter("@TermTaxonomyId", TermTaxonomyId));
            SqlParameter[] sqlparam = ls.ToArray();
            DataTable dt = SqlHelper.ExecuteDataTable(SqlHelper.WpConnectString, CommandType.StoredProcedure, "wp_SelectTblTermRelationship", sqlparam);
            if (dt != null && dt.Rows.Count > 0)
                for (int i = 0; i < dt.Rows.Count; i++)
                    lsresult.Add(GetTblTermRelationshipsEntity(dt.Rows[i]));
            return lsresult;
        }
        public DataTable SelectAllRDT()
        {
            return SqlHelper.ExecuteDataTable(SqlHelper.WpConnectString, CommandType.StoredProcedure, "wp_SelectTblTermRelationshipsAll", null);
        }
        public List<TblTermRelationshipsEntity> SelectAll()
        {
            List<TblTermRelationshipsEntity> lsresult = new List<TblTermRelationshipsEntity>();
            DataTable dt = SqlHelper.ExecuteDataTable(SqlHelper.WpConnectString, CommandType.StoredProcedure, "wp_SelectTblTermRelationshipsAll", null);
            if (dt != null && dt.Rows.Count > 0)
                for (int i = 0; i < dt.Rows.Count; i++)
                    lsresult.Add(GetTblTermRelationshipsEntity(dt.Rows[i]));
            return lsresult;
        }
    }
}

